// This do-file reproduces regressions reported in Tables 1, 2, A.3, A.4 and A.5 

version 14
use "data/reg_public_goods.dta" , clear



local controls0 
local controls1 age gender l_stay  bgy_pop urban  nb_families 
local controls2 `controls1' edu_*
local controls3 `controls2' occ_* occ1_*
local controls4 `controls3' pcinc poor  
	



foreach y in edge   {
local append replace
foreach x in WeightedOutcome bgy_elem_school bgy_high_school bgy_market bgy_healthcentre bgy_waterworks {

	su `x'
	local mean=r(mean)
	local sd=r(sd) 

	xtreg `x'     herf_community_`y', fe cluster(group_mun)
	outreg2 using "results/table_1_panel_a", 2aster addstat("Mean Dep. Var."  , `mean' , "Std. Dev. Dep. Var." ,  `sd')      bdec(2) sdec(2)    nocons keep(herf_community_`y' )  `append'

	xtreg `x'  `controls4'   herf_community_`y', fe cluster(group_mun)
	outreg2 using "results/table_1_panel_b",  addstat("Mean Dep. Var."  , `mean' , "Std. Dev. Dep. Var." ,  `sd')      bdec(2) sdec(2) 2aster   nocons keep(herf_community_`y' )  `append'
	local append append

	}
}

// ROBUSTNESS CHECKS


	g herf = herf_community_edge_45

	xtreg  WeightedOutcome `controls4'  herf  ,fe cluster(group_mun) 
	outreg2 using "results/table_2",        bdec(2) sdec(2)  2aster  nocons keep(herf )  replace

	drop herf 
	g herf = herf_community_edge

	*iv 
	xtivreg WeightedOutcome   `controls4' (herf = herf_community_edge_45)  , fe vce(robust)
	outreg2 using "results/table_2",        bdec(2) sdec(2) 2aster   nocons keep(herf )  append

	* weighted by family size
	drop herf 
	g herf = w_herf_community_edge
	
	xtreg  WeightedOutcome `controls4'  herf  ,fe cluster(group_mun) 
	outreg2 using "results/table_2",        bdec(2) sdec(2)  2aster  nocons keep(herf )  append

	* weighted by number of voters
	drop herf 
	g herf = herf_community_by_indiv
	xtreg  WeightedOutcome `controls4'  herf  ,fe cluster(group_mun) 
	outreg2 using "results/table_2",        bdec(2) sdec(2) 2aster   nocons keep(herf )  append

	* walktrap
	drop herf 
	g herf = herf_community_walktrap
	
	xtreg   WeightedOutcome `controls4'  herf  ,fe cluster(group_mun) 
	outreg2 using "results/table_2",        bdec(2) sdec(2)  2aster  nocons keep(herf )  append
	
	drop herf


	
foreach y in herf_community_edge      {
local append replace
foreach x in  WeightedOutcome {


	* Drop URBAN
	xtreg `x'  `controls4'  `y' if urban==0 , fe cluster(group_mun)
	outreg2 using "results/table_a_4",        bdec(2) sdec(2)   2aster nocons keep(`y' ) replace

	* Drop Hometown
	xtreg `x'  `controls4'  `y' if hometown==0 , fe cluster(group_mun)
	outreg2 using "results/table_a_4",        bdec(2) sdec(2) 2aster   nocons keep(`y' )  append

	* Exclude ARMM
	xtreg `x'  `controls4'  `y' if armm==0 , fe cluster(group_mun)
	outreg2 using "results/table_a_4",        bdec(2) sdec(2)  2aster  nocons keep(`y' )  append

/*	
	* Incumbent characteristics
	xtreg `x'  `controls4' `controls_incumbent'  `y'  , fe cluster(group_mun)
	outreg2 using "results/table_a_4",        bdec(2) sdec(2) 2aster   nocons keep(`y' )  append
	
	* Incumbent and challenger characteristics
	xtreg `x'  `controls4' `controls_incumbent' `controls_challenger'  `y'  , fe cluster(group_mun)
	outreg2 using "results/table_a_4",        bdec(2) sdec(2)  2aster  nocons keep(`y' )  append
*/
	
	* Ethnic and Religious Fragmentation
	xtreg `x'  `controls4' herf_ethnic herf_religion `y'  , fe cluster(group_mun)
	outreg2 using "results/table_a_4",        bdec(2) sdec(2)  2aster  nocons keep(`y'      )  append

	

	
}
}



// exclude outliers (Structure)

foreach y in edge        {
local append replace
foreach i in 1 5   {
	local j = 100 - `i'
	qui su herf_community_`y' , d
	local p =r(p`i') 
	local t = r(p`j')
	
		xtreg WeightedOutcome  `controls4'  herf_community_`y' if herf_community_`y' > `p' & herf_community_`y' < `t' , fe cluster(group_mun)
		outreg2 using "results/table_a_3",        bdec(2) sdec(2)  2aster  nocons keep(herf_community_`y' )  `append'

		local append append

		
}
}
// exclude outliers (population)
foreach y in edge        {
local append append
foreach i in 1 5   {
	local j = 100 - `i'

	qui su bgy_pop , d
	local p =r(p`i') 
	local t = r(p`j')

	
		xtreg WeightedOutcome  `controls4'  herf_community_`y' if bgy_pop > `p'  &  bgy_pop < `t' , fe cluster(group_mun)
		outreg2 using "results/table_a_3",        bdec(2) sdec(2) 2aster   nocons keep(herf_community_`y' )  `append'

		local append append

		
}
}





	su herf_ethnic  ,d 
	local p50=r(p50)
	g above = (herf_ethnic > `p50')
	g herf_community_below = herf_community_edge*(herf_ethnic< `p50')
	g herf_community_above = herf_community_edge*(herf_ethnic >  `p50')

	xtreg WeightedOutcome  `controls4' herf_community_below herf_community_above  above , fe cluster(group_mun)
	outreg2 using "results/table_a_5_1",        bdec(2) sdec(2) 2aster  nocons keep(herf_community_below herf_community_above   )  replace

	capture drop herf_community_below herf_community_above  abov
	su herf_religion  ,d 
	local p50=r(p50)
	g above = (herf_religion > `p50')
	g herf_community_below = herf_community_edge*(herf_religion< `p50')
	g herf_community_above = herf_community_edge*(herf_religion >  `p50')


	xtreg WeightedOutcome  `controls4' herf_community_below herf_community_above  , fe cluster(group_mun)
	outreg2 using "results/table_a_5_1",        bdec(2) sdec(2) 2aster   nocons keep(herf_community_below herf_community_above   )  append

	
	xtreg WeightedOutcome  `controls4'  herf_community_edge   if armm==1, fe cluster(group_mun)
	outreg2 using "results/table_a_5_1",        bdec(2) sdec(2) 2aster  nocons keep(herf_community_edge  )  append	
	
	

